package com.pay.controller;

import com.pay.pojo.User;
import com.pay.service.AccountService;
import com.remote.api.EmailApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/api/user")
public class UserController {

   @Reference
   private EmailApi emailApi;

   @Resource
   private AccountService accountService;

    @PostMapping("/create/code")
    public void sendEmail(String email) {
        //        // 发送邮件时间很慢（可以使用异步方式发送：多线程、消息队列）
        Integer checkCode = new Random().nextInt(1000000);
        String activationCode =   checkCode.toString();
        // 生成的验证码需要保存到数据库
        emailApi.sendEmail(email,String.valueOf(checkCode));
        Map<Object,Object> resultMap = new HashMap<>();
        resultMap.put("code", 200);
        resultMap.put("message", "注册成功,请前往登陆!");
    }

    @PostMapping("/create/account")
    public void createAccount(User user){
        accountService.createAccount(user);
    }

    @PostMapping("/login")
    public Map<Object,Object> login(User user){
        return accountService.loginAccount(user);
    }
}
